package com.glory.dmegcpda.viewmodel

import android.os.Bundle
import androidx.databinding.ObservableArrayList
import com.glory.dmegcpda.BR
import com.glory.dmegcpda.R
import com.glory.dmegcpda.model.KeepRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glory.mvvmnet.entity.ToastMessageBean
import com.glory.dmegcpda.bean.CheckListEntity
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: CheckTaskListViewModel
 * Author: XJH
 * Date: 2021/10/21 11:38
 * Description:
 * History:
 */
class KeepTaskListViewModel : BaseViewModel() {
//    "保养表单版本",  "上次保养版本",  "上次保养人",  "上次是否合格", "上次保养单状态",
    val keepListTitle = arrayListOf(
        "序号",
        "设备类型",
        "设备编号",
        "保养表单名称",
        "状态",
        "上次保养时间",
        "即将保养时间",
        "距离时间(天)"
    )

    val keepRepository by lazy { KeepRepository() }
    val eqpType = arrayListOf<String>()

    private val onCheckTaskClick: CheckTaskListViewModel.OnClickListener =
        object : CheckTaskListViewModel.OnClickListener {
            override fun onChildClick(item: Any) {
                val entity = item as CheckListEntity.DataListEntity
                val bundle = Bundle()
                bundle.putString("SHEETRRN", entity.FORMRRN.toString())
                bundle.putString("OBJECTRRN", entity.OBJECTRRN.toString())
                bundle.putString("EQPID", entity.EQUIPMENTID)
                bundle.putString("SHEETNAME", entity.SHEETNAME)
                if (entity.STATUS == "FORM" || entity.STATUS == "START") {
                    defUI.startActivity.value = bundle
                } else {
                    val map = JsonObject()
                    map.addProperty("ACTIONTYPE", "START")
                    map.addProperty("SCHEDULERRN", entity.OBJECTRRN.toString())
                    launchOnlyResult({
                        keepRepository.startKeep(map)
                        defUI.callObserve.value = CallObserveBean(200,0)
                    })
                }
            }

        }

    //order 批次列表
    var checkItems = ObservableArrayList<CheckListEntity.DataListEntity>()
    var checkBinding =
        ItemBinding.of<CheckListEntity.DataListEntity>(
            BR.checkTaskItem,
            R.layout.item_check_task_list
        )
            .bindExtra(BR.checkTaskClick, onCheckTaskClick)


    fun searchCheckList(eqp: String, eqpType: String, formName: String, time: String) {
        checkItems.clear()
        val map = JsonObject()
        map.addProperty("ENTITYMODEL", "com.glory.pms.model.AbstractPmsSchedule")
        var whereClause = StringBuffer()
        whereClause.append("pmsType ='PM'")
        if (eqp.isNotEmpty()){
            whereClause.append(" AND equipmentId='$eqp'")
        }
        if (eqpType.isNotEmpty()){
            whereClause.append(" AND equipmentType ='$eqpType'")
        }
        if (formName.isNotEmpty()){
            whereClause.append(" AND sheetName ='$formName'")
        }
        map.addProperty("WHERECLAUSE", whereClause.toString())
        launchOnlyResult({
            val checkList = keepRepository.getKeepList(map)
            if (checkList.DATALIST.isEmpty()) {
                defUI.toastEvent.value = ToastMessageBean("服务器未返回数据", false)
                return@launchOnlyResult
            }
            val keepList = arrayListOf<CheckListEntity.DataListEntity>()
            checkList.DATALIST.forEach {
                if (it.STATUS != "CANCEL" && it.STATUS != "COMPLETE") {
                    it.POSITION = keepList.size + 1
                    keepList.add(it)
                }
            }
            if (time.isEmpty()) {
                checkItems.addAll(keepList)
            } else {
                keepList.forEach {
                    if (it.CHECKINTERVALVALUE == time.toInt()) {
                        checkItems.add(it)
                    }
                }
            }
        })

    }

    fun getEqpType() {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "GETBYREFNAME")
        map.addProperty("REFNAME", "EquipmentTypePM")
        launchOnlyResult({
            val entity = keepRepository.getEqpType(map)
            entity.ADUREFLIST.forEach {
                eqpType.add(it.KEYID)
            }
        })
    }


}